//
//  CYButtonView.m
//  CreditAddressBook
//
//  Created by LE on 15/12/15.
//  Copyright © 2015年 Lee. All rights reserved.
//

#import "CYButtonView.h"
#import "CYDisTabBarButton.h"
#import "UIView+Extension.h"

static CYClickedBlock _block;

@implementation CYButtonView

- (void)addTabBarButtonWithImageName:(NSString *)imageName title:(NSString *)title description:(NSString *)description showLines:(NSArray *)statuses showstatus:(NSString *)statusimage clicked:(CYClickedBlock)clicked {
    CYDisTabBarButton *tabBar = [CYDisTabBarButton loadFromNibUsingClassName];
    if (imageName) {
        tabBar.iconImageView.image = [UIImage imageNamed:imageName];
    }
    if (statusimage) {
        tabBar.statusImageView.image = [UIImage imageNamed:statusimage];
    }
    tabBar.titleLabel.text = title;
    tabBar.descriptionLabel.text = description;
    if (clicked) {
        tabBar.block = clicked;
    }
//    [tabBar.button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
//    _block = [clicked copy];
    for (NSNumber *status in statuses) {
        if (status.integerValue == CYDirectionStatusTop) {
            tabBar.topLine.hidden = NO;
        } else if (status.integerValue == CYDirectionStatusLeft) {
            tabBar.leftLine.hidden = NO;
        } else if (status.integerValue == CYDirectionStatusBottom) {
            tabBar.bottomLine.hidden = NO;
        } else if (status.integerValue == CYDirectionStatusRight) {
            tabBar.rightLine.hidden = NO;
        }
    }
    [self addSubview:tabBar];
    [self layoutSubviews];
}

//- (void)btnClick:(UIButton *)btn {
//    _block();
//}

- (void)layoutSubviews {
    [super layoutSubviews];
    CGFloat count = self.subviews.count;
    CGFloat tabBarW = self.frame.size.width/count;
    CGFloat tabBarH = self.frame.size.height;
    CGFloat tabBarY = 0;
    for (int i = 0; i < count; i++) {
        UIButton *tabBtn = self.subviews[i];
        tabBtn.tag = i;
        CGFloat tabBarX = tabBarW * i;
        tabBtn.frame = CGRectMake(tabBarX, tabBarY, tabBarW, tabBarH);
    }
}

@end
